import JoyStick, { IJoyStickType } from "./JoyStick";
import { JoyStickTypeBase } from "./TypeBase";

export default class JoyStickTypeDelta extends JoyStickTypeBase implements IJoyStickType {
    constructor(stick: JoyStick) {
        super(stick)
    }
    touchStartEvent(event: cc.Event.EventTouch): boolean {
        this._stick.show.active = true
        const touchPos = this._stick.node.convertToNodeSpaceAR(event.getLocation())
        this._stick._stickPos = touchPos
        this._stick.show.active = true
        this._stick._touchLocation = event.getLocation()

        this._stick.ring.setPosition(touchPos)
        this._stick.dot.setPosition(touchPos)
        return true
    }
    touchMoveEvent(event: cc.Event.EventTouch): boolean {
        const touchPos = this._stick.ring.convertToNodeSpaceAR(event.getLocation())
        const r = touchPos.mag() / this._stick._radius
        if (r < this._stick._minDistancePercent) {
            this._stick.ring.color = cc.Color.RED
        } else {
            this._stick.ring.color = cc.Color.GRAY
        }
        return true
    }
    touchEndEvent(event: cc.Event.EventTouch): boolean {
        this._stick.show.active = false
        return true
    }
    init() {
        this._stick.show.active = false
        this._stick.ring.width = this._stick.dot.width * 1.5
        this._stick.ring.height = this._stick.dot.height * 1.5
        this._stick.ring.color = cc.Color.GRAY
        this._stick._radius = Math.max(this._stick.node.width, this._stick.node.height)
        this._stick._minDistancePercent = 0.1
    }
}
